package com.dianrui.fmi.order;


import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.dianrui.fmi.R;
import com.dianrui.fmi.activity.BaseActivity;
import com.dianrui.fmi.adapter.DelegationAdapter;
import com.dianrui.fmi.adapter.PostionListAdapter;
import com.dianrui.fmi.adapter.SettlementAdapter;
import com.dianrui.fmi.bean.Delegation;
import com.dianrui.fmi.bean.PositionBean;
import com.dianrui.fmi.util.MyUtil;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */

public class MyOrderAcitivty extends BaseActivity {
    private String type="1";
    private List<Delegation> mDelegationList = new ArrayList<>();
    private List<PositionBean> mPositionList = new ArrayList<>();
    private List<PositionBean> mtrList = new ArrayList<>();
    private DelegationAdapter delegationAdapter;
    private PostionListAdapter postionListAdapter;
    private SettlementAdapter settlementAdapter;


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.right_img)
    ImageView rightImg;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.buy_txt)
    TextView buyTxt;
    @BindView(R.id.buy_line)
    View buyLine;
    @BindView(R.id.tr_txt)
    TextView trTxt;
    @BindView(R.id.tr_line)
    View trLine;
    @BindView(R.id.delegation_txt)
    TextView delegationTxt;
    @BindView(R.id.delegation_line)
    View delegationLine;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;


    @Override
    public int getContentViewId() {
        return R.layout.activity_order;
    }

    @Override
    public void initView() {
        rightImg.setImageResource(R.mipmap.caldanar_img);
        rightImg.setVisibility(View.VISIBLE);
        title.setText(getString(R.string.my_order));
        delegationAdapter=new DelegationAdapter(MyOrderAcitivty.this,mDelegationList);
        listview.setAdapter(delegationAdapter);
        getDelegationList();
    }

    //获取委托列表
    private void getDelegationList() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("type", "1");
        jsonObject.addProperty("is_real", "1");
        OkHttpUtils.getInstance().postWithMemberToken(Url.MODEORDER, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (mDelegationList != null) {
                                    mDelegationList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (null != jsonArray && jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        Delegation delegation = new Delegation();
                                        delegation.order_id = jsonArray.optJSONObject(i).optString("order_id");
                                        delegation.full_symbol = jsonArray.optJSONObject(i).optString("full_symbol");
                                        delegation.symbol = jsonArray.optJSONObject(i).optString("symbol");
                                        delegation.mix_price = jsonArray.optJSONObject(i).optString("mix_price");
                                        delegation.name = jsonArray.optJSONObject(i).optString("name");
                                        delegation.number = jsonArray.optJSONObject(i).optString("number");
                                        delegation.start_price = jsonArray.optJSONObject(i).optString("start_price");
                                        delegation.price = jsonArray.optJSONObject(i).optString("price");
                                        delegation.earnings = jsonArray.optJSONObject(i).optString("earnings");
                                        delegation.createtime = jsonArray.optJSONObject(i).optString("create_time");
                                        delegation.overnight_fee = jsonArray.optJSONObject(i).optString("overnight_fee");
                                        delegation.direction = jsonArray.optJSONObject(i).optString("direction");
                                        mDelegationList.add(delegation);
                                    }
                                    if (delegationAdapter != null) {
                                        delegationAdapter.refresh(mDelegationList);
                                    }
                                    listview.setVisibility(View.VISIBLE);
                                    emptyView.setVisibility(View.GONE);

                                } else {
                                    listview.setVisibility(View.GONE);
                                    emptyView.setVisibility(View.VISIBLE);

                                }
                            } else {
                                listview.setVisibility(View.GONE);
                                emptyView.setVisibility(View.VISIBLE);

                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    private void getPostionList() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("type", "2");
        jsonObject.addProperty("is_real", "1");
        OkHttpUtils.getInstance().postWithMemberToken(Url.MODEORDER, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (mPositionList != null) {
                                    mPositionList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        PositionBean positionBean = new PositionBean();
                                        positionBean.order_id = jsonArray.optJSONObject(i).optString("order_id");
                                        positionBean.createtime = jsonArray.optJSONObject(i).optString("create_time");
                                        positionBean.price = jsonArray.optJSONObject(i).optString("price");
                                        positionBean.start_price = jsonArray.optJSONObject(i).optString("start_price");
                                        positionBean.number = jsonArray.optJSONObject(i).optString("number");
                                        positionBean.earnings = jsonArray.optJSONObject(i).optString("earnings");
                                        positionBean.full_symbol = jsonArray.optJSONObject(i).optString("full_symbol");
                                        positionBean.symbol = jsonArray.optJSONObject(i).optString("symbol");
                                        positionBean.number = jsonArray.optJSONObject(i).optString("number");
                                        positionBean.name = jsonArray.optJSONObject(i).optString("name");
                                        positionBean.direction = jsonArray.optJSONObject(i).optString("direction");
                                        positionBean.service_charge = jsonArray.optJSONObject(i).optString("service_charge");
                                        positionBean.mix_price = jsonArray.optJSONObject(i).optString("mix_price");
                                        positionBean.postion = jsonArray.optJSONObject(i).optString("overnight_fee");
                                        positionBean.margin = jsonArray.optJSONObject(i).optString("margin");
                                        positionBean.total = jsonArray.optJSONObject(i).optString("total");
                                        mPositionList.add(positionBean);
                                    }
                                    if (postionListAdapter != null) {
                                        postionListAdapter.refresh(mPositionList);
                                    }
                                    listview.setVisibility(View.VISIBLE);
                                    emptyView.setVisibility(View.GONE);

                                } else {
                                    listview.setVisibility(View.GONE);
                                    emptyView.setVisibility(View.VISIBLE);


                                }
                            } else {
                                listview.setVisibility(View.GONE);
                                emptyView.setVisibility(View.VISIBLE);
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    listview.setVisibility(View.GONE);

                }
            }
        });
    }

    private void getTrList() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("type", "3");
        jsonObject.addProperty("is_real", "1");
        OkHttpUtils.getInstance().postWithMemberToken(Url.MODEORDER, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (mtrList != null) {
                                    mtrList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (null != jsonArray && jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        PositionBean positionBean = new PositionBean();
                                        positionBean.order_id = jsonArray.optJSONObject(i).optString("order_id");
                                        positionBean.createtime = jsonArray.optJSONObject(i).optString("create_time");
                                        positionBean.price = jsonArray.optJSONObject(i).optString("price");
                                        positionBean.end_time = jsonArray.optJSONObject(i).optString("end_time");
                                        positionBean.number= jsonArray.optJSONObject(i).optString("number");
                                        positionBean.earnings= jsonArray.optJSONObject(i).optString("earnings");
                                        positionBean.start_price = jsonArray.optJSONObject(i).optString("start_price");
                                        positionBean.full_symbol= jsonArray.optJSONObject(i).optString("full_symbol");
                                        positionBean.symbol= jsonArray.optJSONObject(i).optString("symbol");
                                        positionBean.name= jsonArray.optJSONObject(i).optString("name");
                                        positionBean.number= jsonArray.optJSONObject(i).optString("number");
                                        positionBean.service_charge = jsonArray.optJSONObject(i).optString("service_charge");
                                        positionBean.mix_price = jsonArray.optJSONObject(i).optString("mix_price");
                                        positionBean.direction= jsonArray.optJSONObject(i).optString("direction");
                                        positionBean.postion = jsonArray.optJSONObject(i).optString("overnight_fee");
                                        positionBean.margin = jsonArray.optJSONObject(i).optString("margin");
                                        positionBean.total = jsonArray.optJSONObject(i).optString("total");
                                        mtrList.add(positionBean);
                                    }
                                    if (settlementAdapter != null) {
                                        settlementAdapter.refresh(mtrList);
                                    }
                                    listview.setVisibility(View.VISIBLE);
                                    emptyView.setVisibility(View.GONE);
                                } else {
                                    listview.setVisibility(View.GONE);
                                    emptyView.setVisibility(View.VISIBLE);
                                }
                            }else {
                                listview.setVisibility(View.GONE);
                                emptyView.setVisibility(View.VISIBLE);

                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.right_img, R.id.buy_layout, R.id.tr_layout, R.id.delegation_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.delegation_layout:
                delegationTxt.setTextColor(getResources().getColor(R.color.red1));
                delegationLine.setVisibility(View.VISIBLE);
                buyTxt.setTextColor(getResources().getColor(R.color.black_333333));
                buyLine.setVisibility(View.INVISIBLE);
                trTxt.setTextColor(getResources().getColor(R.color.black_333333));
                trLine.setVisibility(View.INVISIBLE);
                delegationAdapter=new DelegationAdapter(MyOrderAcitivty.this,mDelegationList);
                listview.setAdapter(delegationAdapter);
                getDelegationList();
                break;
            case R.id.buy_layout:
                delegationTxt.setTextColor(getResources().getColor(R.color.black_333333));
                delegationLine.setVisibility(View.INVISIBLE);
                buyTxt.setTextColor(getResources().getColor(R.color.red1));
                buyLine.setVisibility(View.VISIBLE);
                trTxt.setTextColor(getResources().getColor(R.color.black_333333));
                trLine.setVisibility(View.INVISIBLE);
                postionListAdapter=new PostionListAdapter(MyOrderAcitivty.this,mPositionList);
                listview.setAdapter(postionListAdapter);
                getPostionList();
                postionListAdapter.setOnButtonClickListener(new PostionListAdapter.CloseClickListener() {
                    @Override
                    public void onPostionsListCloseClick(String id) {
                        showClose(id);
                    }

                });
                break;
            case R.id.tr_layout:
                buyTxt.setTextColor(getResources().getColor(R.color.black_333333));
                buyLine.setVisibility(View.INVISIBLE);
                trTxt.setTextColor(getResources().getColor(R.color.red1));
                trLine.setVisibility(View.VISIBLE);
                delegationTxt.setTextColor(getResources().getColor(R.color.black_333333));
                delegationLine.setVisibility(View.INVISIBLE);
                settlementAdapter = new SettlementAdapter(MyOrderAcitivty.this, mtrList);
                listview.setAdapter(settlementAdapter);
                getTrList();
                break;
            case R.id.right_img:

                break;

            default:
                break;
        }
    }

    private void showClose(final String id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MyOrderAcitivty.this, R.style.RcDialog);
        View view = LayoutInflater.from(MyOrderAcitivty.this).inflate(R.layout.close_asks, null);
        final AlertDialog dialog = builder.create();
        dialog.setView(view);
        TextView cancel_dialog_ask = view.findViewById(R.id.cancel_dialog_ask);
        final TextView yes_dialog_ask = view.findViewById(R.id.yes_dialog_ask);
        cancel_dialog_ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        yes_dialog_ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ClosePostion(id);
            }
        });
        if (!MyUtil.getActivity(dialog.getContext()).isFinishing()) {
            dialog.show();
        }
    }


    private void ClosePostion(String id) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("order_id", id);
        jsonObject.addProperty("is_real", "1");
        OkHttpUtils.getInstance().postWithMemberToken(Url.CLOSE, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (null != jsonObject) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                 getPostionList();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }
}
